package main


import (
	"github.com/gin-gonic/gin"
	"fmt"
	"io/ioutil"
	//"encoding/json"
	"mhttp/msql"
	"strings"
	"strconv"
)

type Datauser struct {
  	Game  		string 
 	List  		[]string 
  	Channel 	string
  	Logo 		string
  	Model 		string
  	Id 			string
  	Set 		string
  	Old_game	string
  	New_game	string
  	Old_channel	string
  	New_channel	string
  	Old_model	string
  	New_model	string
  	Sum			string
  	Name		string
}


var mdb m_mysql.Mydb

func main() {
	mdb = m_mysql.Open_db("root:anjiu123@/anjiu")
	//fmt.Println(mdb.Db)
	r := gin.Default()
    http_routing(r)
	r.Run() 
}

func http_routing(r *gin.Engine) {
	r.POST("/:url", func(c *gin.Context) {
		url := c.Param("url")
		//data := c.DefaultPostForm("name", "sockstack")
		mbool := get_data(c,url,"post")
		return_http(c,url,mbool)
	})
	r.GET("/:url", func(c *gin.Context) {
		url := c.Param("url")
        mbool := get_data(c,url,"get")
		return_http(c,url,mbool)
	})
}

func get_data(c *gin.Context, url string, way string) *m_mysql.M_code {
    dict := &Datauser{}
    switch url{
    	case "append":
            if way == "get" {
    			dict.Game = c.DefaultQuery("game", "")
    			dict.List = []string{c.DefaultQuery("list", "")}
    			dict.Channel = c.DefaultQuery("channel", "")
    			dict.Logo = c.DefaultQuery("logo", "")
    			dict.Model = c.DefaultQuery("model", "")
            }else{
    			bytedata, _ := ioutil.ReadAll(c.Request.Body)
    			json.Unmarshal(bytedata, &dict)
            }
            return Hppt_Append(dict)
    	case "get":
            if way == "get" {
    			dict.Game = c.DefaultQuery("game", "")
    			dict.Channel = c.DefaultQuery("channel", "")
    			dict.Model = c.DefaultQuery("model", "")
            }else{
    			bytedata, _ := ioutil.ReadAll(c.Request.Body)
    			json.Unmarshal(bytedata, &dict)
            }
            return Hppt_Get(dict)
    	case "del":
            if way == "get" {
            	dict.Id = c.DefaultQuery("id", "")
            }else{
    			bytedata, _ := ioutil.ReadAll(c.Request.Body)
    			json.Unmarshal(bytedata, &dict)
            }
            return Hppt_Del(dict)
    	case "see":
            if way == "get" {
    			dict.Game = c.DefaultQuery("game", "")
    			dict.Set = c.DefaultQuery("set", "")
    			dict.Channel = c.DefaultQuery("channel", "")
    			dict.Model = c.DefaultQuery("model", "")
            }else{
    			dict.Game = c.DefaultPostForm("game", "")
    			dict.Set = c.DefaultPostForm("set", "")
    			dict.Channel = c.DefaultPostForm("channel", "")
    			dict.Model = c.DefaultPostForm("model", "")	
            }
            return Hppt_See(dict)
    	case "set":
            if way == "get" {
    			dict.Old_game = c.DefaultQuery("old_game", "")
    			dict.New_game = c.DefaultQuery("new_game", "")
    			dict.Old_channel = c.DefaultQuery("old_channel", "")
    			dict.New_channel = c.DefaultQuery("new_channel", "")
    			dict.Old_model = c.DefaultQuery("old_model", "")
    			dict.New_model = c.DefaultQuery("new_model", "")
    			dict.Sum = c.DefaultQuery("sum", "")
            }else{
    			dict.Old_game = c.DefaultPostForm("old_game", "")
    			dict.New_game = c.DefaultPostForm("new_game", "")
    			dict.Old_channel = c.DefaultPostForm("old_channel", "")
    			dict.New_channel = c.DefaultPostForm("new_channel", "")
    			dict.Old_model = c.DefaultPostForm("old_model", "")
    			dict.New_model = c.DefaultPostForm("new_model", "")
    			dict.Sum = c.DefaultPostForm("sum", "")	
            }
            return Hppt_Set(dict)
    	case "getanjiu":
            if way == "get" {
    			dict.Game = c.DefaultQuery("game", "")
            }else{
    			bytedata, _ := ioutil.ReadAll(c.Request.Body)
    			json.Unmarshal(bytedata, &dict)
            }
            return Hppt_Getanjiu(dict)
    	case "compack":
            if way == "get" {
    			dict.Name = c.DefaultQuery("name", "")
    			dict.Set = c.DefaultQuery("set", "")
    			dict.Channel = c.DefaultQuery("channel", "")
            }else{
    			dict.Name = c.DefaultPostForm("name", "")
    			dict.Set = c.DefaultPostForm("set", "")
    			dict.Channel = c.DefaultPostForm("channel", "")	
            }
        	return Hppt_Compack(dict)
        case "newadd":
    		bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newAdd(dict)
        case "newget":
        	bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newGet(dict)
        case "newdel":
        	bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newDel(dict)
        case "newsee":
        	bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newSee(dict)
        case "newset":
        	bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newSet(dict)
        case "newcom":
        	bytedata, _ := ioutil.ReadAll(c.Request.Body)
    		json.Unmarshal(bytedata, &dict)
        	return Hppt_newCom(dict)
        case "init":
        	return Hppt_INIT(dict)
    	default:
            if way == "get" {
    			dict.Id = c.DefaultQuery("id", "")
    			dict.Model = c.DefaultQuery("model", "")
            }else{
    			dict.Id = c.DefaultPostForm("id", "")
    			dict.Model = c.DefaultPostForm("model", "")
            }
            return &m_mysql.M_code{}
    }
}



func return_http(c *gin.Context, url string, data *m_mysql.M_code) {
	host := c.GetHeader("Host")
	ip := c.ClientIP()
	fmt.Println("请求头",host,"ip",ip,"结构体",data)
	switch url{
    	case "see":
			c.JSON(200, gin.H{
				data.Msg
			})
    	default:
    		c.JSON(200, gin.H{
				"message": data,
			})
    }

} 


func Hppt_Append(data *Datauser) *m_mysql.M_code {
	map_variable := make(map[string]map[string]string)
	for k,v := range data.List{
		variable := make(map[string]string)
		t := strings.Split(v, ",")
		variable["game"]		= data.Game
		variable["account"]		= t[0]
		variable["password"]	= t[1]
		variable["logo"]		= data.Logo
		variable["channel"]		= data.Channel
		variable["model"]		= data.Model
		map_variable[string(k)] = variable
	}
	ret := mdb.Insert_accanjiu(data.Game, map_variable)
	return ret
}

func Hppt_Get(data *Datauser) *m_mysql.M_code {
	ret := mdb.M_get(data.Game, data.Channel, data.Model)
	return ret
}

func Hppt_Del(data *Datauser) *m_mysql.M_code {
	intVar, _ := strconv.Atoi(data.Id)
	ret := mdb.M_del(intVar)
	return ret
}

func Hppt_See(data *Datauser) *m_mysql.M_code {
	ret := mdb.Select_accanjiu(data.Game, data.Channel, data.Model)
	return ret
}

func Hppt_Set(data *Datauser) *m_mysql.M_code {
	v1 := make(map[string]string)
	v2 := make(map[string]string)
	sum, _ := strconv.Atoi(data.Sum)
	v1["channel"]  	= data.New_channel
	v1["model"]		= data.New_model
	v1["game"] 		= data.New_game
	v2["channel"] 	= data.Old_channel
	v2["model"] 	= data.Old_model
	v2["game"] 		= data.Old_game
	ret := mdb.Update_accanjiu(v1, v2, sum)
	return ret
}

func Hppt_Getanjiu(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_Compack(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newAdd(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newGet(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newDel(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newSee(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newSet(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_newCom(data *Datauser) *m_mysql.M_code {
	return &m_mysql.M_code{}
}

func Hppt_INIT(data *Datauser) *m_mysql.M_code {

	return mdb.InIt_Data()

}